class Shop::SpuCouponsController < ShopController
  before_action :set_spu, only: [:index, :new, :create, :destroy]

  def index
    @spu_coupons = @spu.spu_coupons.order("created_at DESC").page(params[:page]).per(params[:per])
  end

  def new
    @spu_coupon = SpuCoupon.new
  end

  def create
    @spu_coupon = @spu.spu_coupons.build spu_coupon_params
    if @spu_coupon.save
      redirect_to shop_spu_spu_coupons_path(@spu), notice: "保存成功！"
    else
      redirect_to shop_spu_spu_coupons_path(@spu), notice: "保存失败！"
    end
  end
  
  def destroy
    @spu_coupon = SpuCoupon.find params[:id]
    if @spu_coupon.destroy
      redirect_to shop_spu_spu_coupons_path(@spu), notice: "删除成功."
    else
      redirect_to shop_spu_spu_coupons_path(@spu), notice: "删除失败."
    end
  end

  private

  def set_spu
    @spu = Spu.find params[:spu_id]
  end

  def spu_coupon_params
    params.require(:spu_coupon).permit(:coupon_id)
  end
end
